# -*- coding:utf-8 -*-

import docx
from docx import Document
from docx.shared import Inches
import re
import os
import csv
import shutil

source_path = r"word1/"
target_path = r"未匹配/"
list_file = os.listdir(source_path)


def parse_content(doc_path):
    try:
        dict_data = dict()
        document = Document(doc_path)
        code_list = re.findall(r"证券代码：(.*?) ", document.paragraphs[0].text)
        if len(code_list) > 0:
            dict_data['code'] = re.findall(r"证券代码：(.*?) ", document.paragraphs[0].text)[0]
            dict_data['name'] = re.findall(r"证券简称：(.*?)$", document.paragraphs[0].text)[0]
            # dict_data['title'] = document.paragraphs[1].text + document.paragraphs[2].text
            # dict_data['no'] = re.findall(r"编号：(.*?)$", document.paragraphs[3].text)[0]
            if document.paragraphs[1].text != "":
                if document.paragraphs[2].text.find("编号") >= 0:
                    dict_data['title'] = document.paragraphs[1].text
                    dict_data['no'] = re.findall(r"编号：(.*?)$", document.paragraphs[2].text)[0]
                elif document.paragraphs[2].text.find("编号") < 0:
                    dict_data['title'] = document.paragraphs[1].text + document.paragraphs[2].text
                    dict_data['no'] = re.findall(r"编号：(.*?)$", document.paragraphs[3].text)[0]
            elif document.paragraphs[1].text == "":
                # dict_data['title'] = document.paragraphs[2].text + document.paragraphs[3].text
                # dict_data['no'] = re.findall(r"编号：(.*?)$", document.paragraphs[4].text)[0]
                if document.paragraphs[3].text.find("编号") >= 0:
                    dict_data['title'] = document.paragraphs[2].text
                    dict_data['no'] = re.findall(r"编号：(.*?)$", document.paragraphs[3].text)[0]
                elif document.paragraphs[4].text.find("编号") >= 0:
                    dict_data['title'] = document.paragraphs[2].text + document.paragraphs[3].text
                    dict_data['no'] = re.findall(r"编号：(.*?)$", document.paragraphs[4].text)[0]
            tables = document.tables #获取文件中的表格集
            table = tables[0]#获取文件中的第一个表格
            if table.cell(0, 1).text.find("关系活动类别") < 0:
                cell1 = table.cell(0, 1).text
                tmp = re.findall(r"√(.*?)$", cell1)  # □
                for t in cell1.split("□"):
                    if t.find("√") >= 0:
                        dict_data['relation'] = re.findall(r"√(.*?)$", t.strip())[0]
                        break
                dict_data['company_liaison'] = table.cell(0, 3).text
                dict_data['time'] = table.cell(0, 5).text
                dict_data['site'] = table.cell(0, 7).text
                dict_data['liaison'] = table.cell(0, 9).text
                dict_data['content'] = table.cell(0, 11).text
                dict_data['appendix'] = table.cell(0, 13).text
                dict_data['date'] = table.cell(0, 15).text
            else:
                cell1 = table.cell(0, 2).text
                tmp = re.findall(r"√(.*?)$", cell1)  # □
                for t in cell1.split("□"):
                    if t.find("√") >= 0:
                        dict_data['relation'] = re.findall(r"√(.*?)$", t.strip())[0]
                        break
                dict_data['company_liaison'] = table.cell(0, 5).text
                dict_data['time'] = table.cell(0, 8).text
                dict_data['site'] = table.cell(0, 11).text
                dict_data['liaison'] = table.cell(0, 14).text
                dict_data['content'] = table.cell(0, 15).text
                dict_data['appendix'] = table.cell(0, 19).text
                dict_data['date'] = table.cell(0, 22).text
    except:
        print(f"=====error:{doc_path}================")
    return dict_data

def save_csv(dict_data):
    with open("result_222.csv", 'a', encoding="utf-8-sig", newline='') as csvfile:
        fileheader = ["code", "name", "title", "no", "relation", "company_liaison", "time", "site",
                      "liaison", "content", "appendix", "date"]
        writer=csv.DictWriter(csvfile,fieldnames=fileheader)
        writer.writerow(dict_data)



if __name__ == "__main__":
    try:
        os.mkdir(target_path)
    except:
        pass
    num_1 = 0
    num_2 = 0
    num_3 = 0
    for f in list_file:
        file_type = f.split(".")[1]
        if file_type == "docx" or file_type == "DOCX":
            num_3 += 1
            print(f"========{f}=========")
            dict_data = parse_content(source_path + f)
            if len(dict_data) > 0:
                num_1 += 1
                save_csv(dict_data)
            else:
                num_2 == 2
                shutil.move(source_path + f, target_path)
        else:
            shutil.move(source_path + f, target_path)
    print(f"------文件共计：{str(len(list_file))}-----DOCX：{str(num_3)}---提取：{str(num_1)}----未提取：{str(num_2)}------")